let express = require('express');
let router = express.Router();
let noticeDao = require('../../model/admin/notice.js')
let actionLog = require('../../common/action_log');

let returnNoticeRouter = function (io) {

    //渲染界面
    router.get('/', async function (req, res) {
        await res.render('notice/list');
    })

    //获取数据 
    router.get('/list', async function (req, res) {
        let page = req.query.page;
        let length = req.query.limit;
        let start = (page - 1) * length;
        let noticeList = await noticeDao.noticeList(start, length);
        let count = noticeList[1][0].sum;
        let data = noticeList[0];
        await res.json({
            code: 0,
            count: count,
            data: data,
            msg: ''
        })
    })

    //渲染通知公告添加界面

    router.get('/add', async function (req, res) {
        await res.render('notice/add')
    })

    //通知公告添加事件

    router.post('/add', async function (req, res) {
        let host = req.headers.origin;
        let title = req.body.title;
        let noticeType = req.body.noticeType;
        let content = req.body.content;
        let img = req.body.imgurl;
        if (!title) {
            return await res.json({
                status: 203,
                msg: "请输入通知标题"
            })
        }
        if (!noticeType) {
            return await res.json({
                status: 203,
                msg: "请选择通知公告类型"
            })
        }
        if (noticeType === '图') {
            if (!img) {
                return await res.json({
                    status: 203,
                    msg: '请上传通知公告图片'
                })
            }
        } else {
            if (!content) {
                return await res.json({
                    status: 203,
                    msg: '请输入公告内容'
                })
            }
        }
        img = host + img;
        let addNotice = await noticeDao.addNotice(title, noticeType, content, img);
        //根据Id获取详情
        let noticeDetail = await noticeDao.noticeDetail(addNotice.insertId);
        let results = {};
        results.action = 'notice add';
        results.data = noticeDetail;
        io.sockets.emit('message', results);
        //生成操作日志
        let action = 'noticeAdd'
        let actionContent = '新增标题为：“' + title + '”的通知';
        await actionLog.crearteActionLog(req, action, actionContent);
        await res.json({
            status: 200,
            msg: "success"
        })
    })
    //渲染详情界面

    router.get('/detail/:id', async function (req, res) {
        let id = req.params.id;
        //获取公告详情
        let noticeDetail = await noticeDao.noticeDetail(id);
        let detail = {};
        if (noticeDetail.length > 0) {
            detail = noticeDetail[0];
        }
        await res.render('notice/detail', {
            detail
        })
    })


    //渲染修改界面

    router.get('/edit/:id', async function (req, res) {
        let id = req.params.id;
        //获取公告详情
        let noticeDetail = await noticeDao.noticeDetail(id);
        let detail = {};
        if (noticeDetail.length > 0) {
            detail = noticeDetail[0];
        }
        await res.render('notice/edit', {
            detail
        })
    })

    //通知公告修改事件
    router.post('/edit', async function (req, res) {
        let host = req.headers.origin;
        let title = req.body.title;
        let noticeType = req.body.noticeType;
        let content = req.body.content;
        let img = req.body.imgurl;
        let id = req.body.id;
        if (!title) {
            return await res.json({
                status: 203,
                msg: "请输入通知标题"
            })
        }
        if (!noticeType) {
            return await res.json({
                status: 203,
                msg: "请选择通知公告类型"
            })
        }
        if (noticeType === '图') {
            if (!img) {
                return await res.json({
                    status: 203,
                    msg: '请上传通知公告图片'
                })
            }
        } else {
            if (!content) {
                return await res.json({
                    status: 203,
                    msg: '请输入公告内容'
                })
            }
        }
        img = host + img;
        //执行修改sql 语句
        await noticeDao.updateNotice(id, title, noticeType, content, img);
        //获取公告详情
        let noticeDetail = await noticeDao.noticeDetail(id);
        let results = {};
        results.action = 'notice update';
        results.data = noticeDetail;
        io.sockets.emit('message', results);
        //生成操作日志

        let action = 'noticeUpdate'
        let actionContent = '编辑标题为：“' + title + '”的通知';
        await actionLog.crearteActionLog(req, action, actionContent);
        await res.json({
            status: 200,
            msg: 'success'
        })
    })


    //通知公告删除事件

    router.post('/del', async function (req, res) {
        let noticeId = req.body.id;
        let title = req.body.title;
         //获取公告详情
         let noticeDetail = await noticeDao.noticeDetail(noticeId);
         let results = {};
         results.action = 'notice delete';
         results.data = noticeDetail;
         io.sockets.emit('message', results);
         await noticeDao.removeNoitce(noticeId);
         await res.json({
            status: 200,
            msg: 'success'
        })
        //生成操作日志
        let action  ='noticeDel'
        let actionContent  = '删除标题为：'+ title+'的通知';
        await actionLog.crearteActionLog(req,action,actionContent);
       
    })













    return router;
}

module.exports = returnNoticeRouter;